<template>
  <div class="container" ref="echartsRef"></div>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
const echartsRef = ref<HTMLElement>()
let settime: string | number | NodeJS.Timer | null | undefined = null
onMounted(() => {
  //初始化echarts实例init（ dom ，'主题' ，其余参数如：{ renderer:'svg' }）
  const myEcharts = echarts.init(echartsRef.value!)
  //要操作的配置
  const option = {
    title: {
      text: 'Referer of a Website',
      subtext: 'Fake Data',
      left: 'center',
    },
    tooltip: {
      trigger: 'item',
    },
    // legend: {
    //   orient: 'vertical',
    //   left: 'left',
    // },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: 'Search Engine' },
          { value: 735, name: 'Direct' },
          { value: 580, name: 'Email' },
          { value: 484, name: 'Union Ads' },
          { value: 300, name: 'Video Ads' },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  }

  //设置配置
  myEcharts.setOption(option)
  settime = setInterval(() => {
    option.series[0].data.push({
      value: Math.floor(Math.random() * 100),
      name: 'Search' + Math.floor(Math.random() * 100),
    })
    option.series[0].data.splice(0, 1)
    myEcharts.setOption(option)
    console.log(1)
  }, 7000)
})
onUnmounted(() => {
  clearInterval(settime)
})
</script>
